package com.extension.company.view.main;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.extension.company.Constant;
import com.extension.company.admin.R;

import com.extension.company.common.mvp.BaseFragment;
import com.extension.company.common.utils.Base64Utils;
import com.extension.company.common.utils.Utils;
import com.extension.company.contract.main.FragmentMyContract;
import com.extension.company.model.api.Urls;
import com.extension.company.presenter.main.FragmentMyPresenter;
import com.extension.company.common.widget.dialog.DialogOK;
import com.extension.company.view.login.LoginActivity;
import com.extension.company.view.my.businessenter.SellerEnterActivity;
import com.extension.company.view.my.address.AddressListActivity;
import com.extension.company.view.my.cerrification.CertificationActivity;
import com.extension.company.view.my.feedback.FeedBackActivity;
import com.extension.company.view.my.collect.MyCollectionActivity;
import com.extension.company.view.my.onlineorder.OrderWaitPaymentList;
import com.extension.company.view.my.localorder.LocalCityOrderActivity;
import com.extension.company.view.webview.WebViewActivity;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 我的 fragment
 */
public class FragmentMy extends BaseFragment<FragmentMyContract.View, FragmentMyPresenter>
		implements FragmentMyContract.View, DialogOK.DialogOKListener, View.OnClickListener {

	Activity activity;

	@BindView(R.id.m_top)
	LinearLayout m_top;
	@BindView(R.id.btn_loginout)
	Button btn_loginout;
	@BindView(R.id.layout_order_waitpayment)
	LinearLayout layout_order_waitpayment;
	@BindView(R.id.layout_sellerenter)
	LinearLayout layout_sellerenter;
	@BindView(R.id.layout_order_waitdgoods)
	LinearLayout layout_order_waitdgoods;
	@BindView(R.id.layout_order_waitrgoods)
	LinearLayout layout_order_waitrgoods;
	@BindView(R.id.layout_order_waitevaluate)
	LinearLayout layout_order_waitevaluate;
	@BindView(R.id.layout_certification)
	LinearLayout layout_certification;
	@BindView(R.id.layout_address)
	LinearLayout layout_address;
	@BindView(R.id.layout_collection)
	LinearLayout layout_collection;
	@BindView(R.id.layout_callphone)
	LinearLayout layout_callphone;
	@BindView(R.id.layout_credibility)
	LinearLayout layout_credibility;
	@BindView(R.id.layout_feedback)
	LinearLayout layout_feedback;
	@BindView(R.id.fragment_my_same_city_order)
	LinearLayout layout_sameCityOrder;
	@BindView(R.id.fragment_my_head)
	CircleImageView userImg;
	@BindView(R.id.fragment_my_user_name)
	TextView userName;

	private DialogOK dialogOK; // dialog对话框
	private int dialogType;    //0退出，1拨打电话

	@Override
	public FragmentMyContract.View createView() {
		return this;
	}

	@Override
	public FragmentMyPresenter createPresenter() {
		return new FragmentMyPresenter();
	}

	@Override
	public void init() {
		activity = getActivity();
		initView();
		initData();
		initListener();
	}

	@Override
	public int getMainLayout() {
		return R.layout.fragment_my;
	}

	/**
	 * 初始化布局
	 */
	private void initView() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) m_top.getLayoutParams();
			layoutParams.height = Utils.getStatusBarHeight(activity);
			m_top.setLayoutParams(layoutParams);
		}
		dialogOK = new DialogOK(activity, this);
	}

	/**
	 * 初始化数据
	 */
	private void initData() {
		initUserInfo();
	}

	/**
	 * 设置监听
	 */
	private void initListener() {
		btn_loginout.setOnClickListener(this);
		layout_certification.setOnClickListener(this);
		layout_sellerenter.setOnClickListener(this);
		layout_order_waitpayment.setOnClickListener(this);
		layout_order_waitdgoods.setOnClickListener(this);
		layout_order_waitrgoods.setOnClickListener(this);
		layout_order_waitevaluate.setOnClickListener(this);
		layout_address.setOnClickListener(this);
		layout_collection.setOnClickListener(this);
		layout_callphone.setOnClickListener(this);
		layout_credibility.setOnClickListener(this);
		layout_feedback.setOnClickListener(this);
		layout_sameCityOrder.setOnClickListener(this);
	}


	@Override
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.btn_loginout:
				setQuiteDialog();
				break;
			case R.id.layout_order_waitpayment:
				//待付款
				OrderWaitPaymentList.startActivity(activity, 0);
				break;
			case R.id.layout_order_waitdgoods:
				//待发货
				OrderWaitPaymentList.startActivity(activity, 1);
				break;
			case R.id.layout_order_waitrgoods:
				//待收货
				OrderWaitPaymentList.startActivity(activity, 2);
				break;
			case R.id.layout_order_waitevaluate:
				//待评价
				OrderWaitPaymentList.startActivity(activity, 3);
				break;
			case R.id.layout_callphone:
				setCallDialog();
				break;
			case R.id.layout_certification:
				//实名认证
				CertificationActivity.startActivity(activity);
				break;
			case R.id.layout_sellerenter:
				//商家入驻
				SellerEnterActivity.startActivity(activity);
				break;
			case R.id.layout_address:
				AddressListActivity.startActivity(activity);
				break;
			case R.id.layout_collection:
				MyCollectionActivity.startActivity(activity);
				break;
			case R.id.layout_credibility:
				//诚信经营
				WebViewActivity.startActivity(activity, "诚信经营承诺书", Urls.CredibilityUrl);
				break;
			case R.id.layout_feedback:
				//问题反馈
				FeedBackActivity.startActivity(activity);
				break;
			case R.id.fragment_my_same_city_order: // 同城订单
				Intent intent = new Intent(getContext(), LocalCityOrderActivity.class);
				startActivity(intent);
				break;
			default:
				break;
		}
	}

	@Override
	public void dialogOKListenerOK() {
		if (dialogType == 0) {
			presenter.loginOut(activity); // 请求退出登录
		} else if (dialogType == 1) {
			Intent intent = new Intent(Intent.ACTION_DIAL);
			Uri data = Uri.parse("tel:" + dialogOK.getContent());
			intent.setData(data);
			startActivity(intent);
		}
	}

	@Override
	public void dialogOKListenerCancel() {

	}


	@Override
	public void showLoading() {

	}

	@Override
	public void hideLoading() {

	}

	@Override
	public void onError(Throwable throwable) {

	}


	public void initUserInfo() { // 设置用户的信息
		if (null != Constant.userInfo) {
			userName.setText(Constant.userInfo.getUserName() == null ? "姓名" : Constant.userInfo.getUserName());
			userImg.setImageBitmap(Base64Utils.base64ToBitmap(Constant.userInfo.getUserImg()));
		}

	}

	@Override
	public void LoginOutSuccess() { // 退出登录成功
		LoginActivity.startActivity(activity); // 跳转到登录界面
		activity.finish();
	}

	/**
	 * 退出dialog设置
	 */
	private void setQuiteDialog() {
		dialogType = 0;
		dialogOK.setContent("确定退出登录吗");
		dialogOK.setButtonName("取消", "确定");
		dialogOK.show();
	}

	/**
	 * 打电话dialog
	 */
	private void setCallDialog() {
		dialogType = 1;
		dialogOK.setContent("010-86467815");
		dialogOK.setButtonName("取消", "呼叫");
		dialogOK.show();
	}

}
